package main

import "fmt"

func main() {
	/*
		结构体嵌套：一个结构体中的字段，是另一个结构体类型
			has a 情况下使用
				一个人有一本书、一个人有一个地址
	*/
	b1 := Book{}
	b1.bookName = "西游记"
	b1.price = 45.8

	s1 := Student1{}
	s1.name = "喜羊羊"
	s1.age = 18
	s1.book = b1    //值传递
	fmt.Println(b1) //{西游记 45.8}
	fmt.Println(s1) //{喜羊羊 18 {西游记 45.8}}
	fmt.Printf("学生姓名：%s，学生年龄：%d，看的书：《%s》，书的价格：%.2f\n", s1.name, s1.age, s1.book.bookName, s1.book.price)

	b1.bookName = "红楼梦"
	fmt.Println(b1) //{红楼梦 45.8}
	fmt.Println(s1) //{喜羊羊 18 {西游记 45.8}}

	b2 := Book{"贪吃猫", 24}
	s5 := Student2{}
	s5.name = "Tom"
	s5.age = 8
	s5.book = &b2
	fmt.Println(b2)                                               //{贪吃猫 24}
	fmt.Println(s5)                                               //{Tom 8 0xc0000080a8}
	fmt.Println(s5.name, s5.age, s5.book.bookName, s5.book.price) //Tom 8 贪吃猫 24

	s5.book.bookName = "贪吃猫，飞上天"
	fmt.Println(b2)                                               //{贪吃猫，飞上天 24}
	fmt.Println(s5)                                               //{Tom 8 0xc0000080a8}
	fmt.Println(s5.name, s5.age, s5.book.bookName, s5.book.price) //Tom 8 贪吃猫，飞上天 24

	s2 := Student1{"灰太狼", 36, Book{"Go语言是怎样练成的", 89.7}}
	s3 := Student1{name: "小灰灰", age: 7, book: Book{"这样长大", 36.8}}
	s4 := Student1{
		name: "Jerry",
		age:  6,
		book: Book{
			bookName: "奶酪陷阱",
			price:    12,
		},
	}
	fmt.Println(s2)
	fmt.Println(s3)
	fmt.Println(s4)

}

// 1.定义一个书的结构体
type Book struct {
	bookName string
	price    float64
}

// 2.定义学生的结构体
type Student1 struct {
	name string
	age  int
	book Book
}

type Student2 struct {
	name string
	age  int
	book *Book //book 的地址
}
